<template>
  <el-select
      style="width:100%"
      v-model="dataval"
      @change="selectchange"
      filterable
      clearable
      placeholder="请选择">
    <el-option v-for="(item,i) in list" :key="i" :label="item.label" :value="item.value"></el-option>
    <Page
        style="padding-left:15px;"
        :total="page_data.total"
        :background="background"
        :layout="layout"
        :page.sync="page_data.page"
        :limit.sync="page_data.limit"
        @pagination="init" />
  </el-select>
</template>

<script>
import Page from "@/components/common/Page";
import request from '@/api/request'
export default {
  name: "SelectPlus",
  components:{
    Page
  },
  props: {
    url: {
      type: String,
    },
    selectval:{
      type: [Number, String],
      default:null,
    },
    is_clear:{
      type:Boolean,
      default:false
    },
  },
  watch:{
    is_clear(val){
      if(val){
        this.dataval = null
      }
    }
  },
  data() {
    return {
      dataval: this.selectval,
      list:[],
      page_data: {
        limit: 10,
        page: 1,
        total:10,
      },
      layout:'total,sizes, prev, pager, next',
      background:false,
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init(){
      let param = {limit:this.page_data.limit,page:this.page_data.page,label:this.selectval}
      request({url: this.url, method: 'post', data: param}).then(res => {
        if(res.status == 200){
          this.list = res.data.data
          this.page_data.total = res.data.total
        }
      })
    },
    selectchange(val){
      this.$emit('update:selectval',val)
    }
  }
}
/**
 * // 导入组件
 * import SelectPlus from "@/components/common/SelectPlus";
 * // 使用组件
 * <SelectPlus url="/Admin.Dept/getPageFeild" v-model="selectval" :selectval="selectval" :is_clear="is_clear"></SelectPlus>
 * // 创建接口
 public function getPageFeild(){
    $limit  = $this->request->post('limit', 20, 'intval');
    $page = $this->request->post('page', 1, 'intval');
    //  查询的字段
    $sql ="SELECT dept_id AS value,CONCAT(NAME,'(',note,')') AS label FROM cd_admin_dept";
    //  执行查询操作
    try{
        $res = \think\facade\Db::table('(' . strtolower($sql) . ') as a')
            ->whereRaw($this->whereScope())
            ->paginate(['list_rows'=>$limit,'page'=>$page])
            ->toArray();
    } catch (\Exception $e) {
        throw new ValidateException($e->getMessage());
    }
    $data['status'] = 200;
    $data['data'] = $res;
    return json($data);
}
 */
</script>

<style scoped>

</style>
